﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2QWebProxy.App_Code
{
    public class CacheRecord
    {
        private string requestURI;
        private string userAgent;
        private DateTime expirationDate;
        private DateTime createdDate;
        private string statusCode;
        private string statusDescription;
        private string contentType;
        private int contentLength;
        private Byte[] responseBytes;
        private List<Tuple<String, String>> headers;

        public List<Tuple<String, String>> Headers
        {
            get { return headers; }
            set { headers = value; }
        }

        public Byte[] ResponseBytes
        {
            get { return responseBytes; }
            set { responseBytes = value; }
        }

        public string RequestURI
        {
            get { return requestURI; }
            set { requestURI = value; }
        }

        public string UserAgent
        {
            get { return userAgent; }
            set { userAgent = value; }
        }
        public DateTime ExpirationDate
        {
            get { return expirationDate; }
            set { expirationDate = value; }
        }
        public DateTime CreatedDate
        {
            get { return createdDate; }
            set { createdDate = value; }
        }
        public string StatusCode
        {
            get { return statusCode; }
            set { statusCode = value; }
        }
        public string StatusDescription
        {
            get { return statusDescription; }
            set { statusDescription = value; }
        }

        public string ContentType
        {
            get { return contentType; }
            set { contentType = value; }
        }
        public int ContentLength
        {
            get { return contentLength; }
            set { contentLength = value; }
        }
        //private List<> headers;
    }

    class Cache
    {
        public static CacheRecord SaveCache(List<Tuple<String, String>> requestHeader, List<Tuple<String, String>> reponse)
        {
            CacheRecord newRecord = new CacheRecord();
            //newRecord.ExpirationDate =;
            newRecord.CreatedDate = DateTime.Now;
            newRecord.Headers = reponse;
            foreach (Tuple<String, String> header in requestHeader)
            {
                switch (header.Item1.ToLower())
                {
                    case "host":
                        newRecord.RequestURI = header.Item2;
                        break;
                    case "user-agent":
                        newRecord.UserAgent = header.Item2;
                        break;
                }
            }
            foreach (Tuple<String, String> header in reponse)
            {
                switch (header.Item1.ToLower())
                {
                    case "status-code":
                        newRecord.StatusCode = header.Item2;
                        break;
                    case "status-description":
                        newRecord.StatusDescription = header.Item2;
                        break;
                    case "content-length":
                        if (Int32.Parse(header.Item2) > 0)
                        {
                            newRecord.ResponseBytes = new Byte[Int32.Parse(header.Item2)];
                            newRecord.ContentLength = Int32.Parse(header.Item2);
                        }
                        break;
                    case "content-type":
                            newRecord.ContentType = header.Item2;
                        break;
                }
            }
            return newRecord;
        }
    }
}
